local Vector3 = GLOBAL.Vector3
local require = GLOBAL.require

print("更好的背包加载中")

PrefabFiles = {
	"betterbag",
}

local assets=
{
	Asset("ANIM", "anim/swap_betterbag.zip"),

	Asset("IMAGE", "images/inventoryimages/betterbag.tex"),
	Asset("ATLAS", "images/inventoryimages/betterbag.xml"),

	Asset("IMAGE", "minimap/betterbag.tex"),
	Asset("ATLAS", "minimap/betterbag.xml"),

    Asset("IMAGE", "images/betterbagbg_7x2.tex"),
    Asset("ATLAS", "images/betterbagbg_7x2.xml"),
    Asset("IMAGE", "images/betterbagbg_8x2.tex"),
    Asset("ATLAS", "images/betterbagbg_8x2.xml"),
    Asset("IMAGE", "images/betterbagbg_9x2.tex"),
    Asset("ATLAS", "images/betterbagbg_9x2.xml"),
    Asset("IMAGE", "images/betterbagbg_10x2.tex"),
    Asset("ATLAS", "images/betterbagbg_10x2.xml"),
    Asset("IMAGE", "images/betterbagbg_10x3.tex"),
    Asset("ATLAS", "images/betterbagbg_10x3.xml"),
    Asset("IMAGE", "images/betterbagbg_10x5.tex"),
    Asset("ATLAS", "images/betterbagbg_10x5.xml"),
    Asset("IMAGE", "images/betterbagbg_8x8.tex"),
    Asset("ATLAS", "images/betterbagbg_8x8.xml"),
}

local Ingredient = GLOBAL.Ingredient

--------------------------------------------------------------------------
TUNING.BETTERBAG_LANG         = GetModConfigData("LANGUAGE")
TUNING.BETTERBAG_SLOT         = GetModConfigData("SLOT")
TUNING.BETTERBAG_FRESH        = GetModConfigData("FRESH")
TUNING.BETTERBAG_TEMPERATURE  = GetModConfigData("TEMPLATURE")
TUNING.BETTERBAG_LIGHT        = GetModConfigData("LIGHT")
TUNING.BETTERBAG_WALKSPEED    = GetModConfigData("WALLKSPEED")
TUNING.BETTERBAG_REGENERATION = GetModConfigData("REGENERATION")
TUNING.BETTERBAG_HEALTH_REGEN = GetModConfigData("HEALTH_REGEN")
TUNING.BETTERBAG_HUNGER_REGEN = GetModConfigData("HUNGER_REGEN")
TUNING.BETTERBAG_SANITY_REGEN = GetModConfigData("SANITY_REGEN")
TUNING.BETTERBAG_HEALTH_REGEN_INTERVAL = GetModConfigData("HEALTH_REGEN_INTERVAL")
TUNING.BETTERBAG_HUNGER_REGEN_INTERVAL = GetModConfigData("HUNGER_REGEN_INTERVAL")
TUNING.BETTERBAG_SANITY_REGEN_INTERVAL = GetModConfigData("SANITY_REGEN_INTERVAL")
--------------------------------------------------------------------------

AddMinimapAtlas("minimap/betterbag.xml")

local tec = GLOBAL.TECH.NONE
local rcp = {
    Ingredient("goldnugget", 20), 
    Ingredient("pigskin", 10), 
    Ingredient("slurper_pelt", 5), 
    Ingredient("rope", 5), 
    Ingredient("beefalowool", 5),
    Ingredient("eyebrellahat", 1),
    Ingredient("heatrock", 1)
}

local betterbag = AddRecipe("betterbag", -- name
rcp, -- ingredients Add more like so , {Ingredient("boards", 1), Ingredient("rope", 2), Ingredient("twigs", 1), etc}
GLOBAL.RECIPETABS.REFINE, -- tab ( FARM, WAR, DRESS etc)
tec, -- level (GLOBAL.TECH.NONE, GLOBAL.TECH.SCIENCE_ONE, etc)
nil, -- placer
nil, -- min_spacing
nil, -- nounlock
nil, -- numtogive
nil, -- builder_tag
"images/inventoryimages/betterbag.xml", -- atlas
"betterbag.tex")

local params = {}
local containers = require("containers")

local containers_widgetsetup = containers.widgetsetup or function() return true end

function containers.widgetsetup(container, prefab, data, ...)
    local tt = prefab or container.inst.prefab
    if tt == "betterbag" then
        local t = params[tt]
        if t ~= nil then
            for k, v in pairs(t) do
                container[k] = v
            end
            container:SetNumSlots(container.widget.slotpos ~= nil and #container.widget.slotpos or 0)
        end
    else
        return containers_widgetsetup(container, prefab, data, ...)
    end
end


params.betterbag =
{
    widget =
    {
        slotpos = {},
        animbank = nil,
        animbuild = nil,
        bgatlas = "images/betterbagbg_8x8.xml",
        bgimage = "betterbagbg_8x8.tex",
        pos = Vector3(-200,-60,0),
    },
    issidewidget = true,
    type = "pack",
}

-- 格子设定

local gridsize = 66  -- 每个格子的大小
local slot_gap = 4   -- 格子之间的间隙
local base_offset_x = -160  -- 基础X偏移（靠左）
local base_offset_y = 250    -- 基础Y偏移（靠上）
-- 根据配置确定行数和列数 (x是行数，y是列数)
local rows, cols
if TUNING.BETTERBAG_SLOT == 0 then      -- 14格(7行*2列)
    rows, cols = 7, 2
    params.betterbag.widget.bgatlas = "images/betterbagbg_7x2.xml"
    params.betterbag.widget.bgimage = "betterbagbg_7x2.tex"
    params.betterbag.widget.pos = Vector3(-100, -100, 0)
    base_offset_x = -68
    base_offset_y = 246
elseif TUNING.BETTERBAG_SLOT == 1 then  -- 16格(8行*2列)
    rows, cols = 8, 2
    params.betterbag.widget.bgatlas = "images/betterbagbg_8x2.xml"
    params.betterbag.widget.bgimage = "betterbagbg_8x2.tex"
    params.betterbag.widget.pos = Vector3(-100, -100, 0)
    base_offset_x = -68
    base_offset_y = 276
elseif TUNING.BETTERBAG_SLOT == 2 then  -- 18格(9行*2列)
    rows, cols = 9, 2
    params.betterbag.widget.bgatlas = "images/betterbagbg_9x2.xml"
    params.betterbag.widget.bgimage = "betterbagbg_9x2.tex"
    params.betterbag.widget.pos = Vector3(-100, -130, 0)
    base_offset_x = -68
    base_offset_y = 320
elseif TUNING.BETTERBAG_SLOT == 3 then  -- 20格(10行*2列)
    rows, cols = 10, 2
    params.betterbag.widget.bgatlas = "images/betterbagbg_10x2.xml"
    params.betterbag.widget.bgimage = "betterbagbg_10x2.tex"
    params.betterbag.widget.pos = Vector3(-100, -130, 0)
    base_offset_x = -68
    base_offset_y = 350
elseif TUNING.BETTERBAG_SLOT == 4 then  -- 30格(10行*3列)
    rows, cols = 10, 3
    params.betterbag.widget.bgatlas = "images/betterbagbg_10x3.xml"
    params.betterbag.widget.bgimage = "betterbagbg_10x3.tex"
    params.betterbag.widget.pos = Vector3(-100, -130, 0)
    base_offset_x = -103
    base_offset_y = 353
elseif TUNING.BETTERBAG_SLOT == 5 then  -- 50格(10行*5列)
    rows, cols = 10, 5
    params.betterbag.widget.bgatlas = "images/betterbagbg_10x5.xml"
    params.betterbag.widget.bgimage = "betterbagbg_10x5.tex"
    params.betterbag.widget.pos = Vector3(-170, -130, 0)
    base_offset_x = -175
    base_offset_y = 350
else
    rows, cols = 8, 8
    params.betterbag.widget.bgatlas = "images/betterbagbg_8x8.xml"
    params.betterbag.widget.bgimage = "betterbagbg_8x8.tex"
    params.betterbag.widget.pos = Vector3(-200,-60,0)
    base_offset_x = -280
    base_offset_y = 274
end

-- 清空原有位置
params.betterbag.widget.slotpos = {}

-- 计算第一格位置（左上角）
local first_cell_x = base_offset_x + gridsize/2
local first_cell_y = base_offset_y - gridsize/2

-- 垂直排列 (从上到下，然后从左到右)
for col = 0, cols-1 do
    for row = 0, rows-1 do
        local x = first_cell_x + col * (gridsize + slot_gap)
        local y = first_cell_y - row * (gridsize + slot_gap)
        table.insert(params.betterbag.widget.slotpos, Vector3(x, y, 0))
    end
end

function params.betterbag.itemtestfn(container, item, slot)
    if item:HasTag("betterbag") then
        return false
    end
    return true
end

containers.MAXITEMSLOTS = math.max(containers.MAXITEMSLOTS, params.betterbag.widget.slotpos ~= nil and #params.betterbag.widget.slotpos or 0)

print("更好的背包加载完成")
